﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ece750project
{
    public struct Finance_para_type
    {
        public int Finance_id;
        public int select_doc_ID;
    }
    public partial class Finance : Form
    {
        public Finance_para_type para;
        public Finance()
        {
            InitializeComponent();
        }

        private void Finance_Load(object sender, EventArgs e)
        {
            lbl_staf_name.Text  = DB_BusinessRules.get_person_name(para.Finance_id);
            Fill_list_doc();
        }
        private void Fill_list_doc()
        {
            DataTable DT = DB_BusinessRules.get_Active_doc_list();
            listDocs.Items.Clear();
            if (DT == null) return;
            for (int i = 0; i <= DT.Rows.Count - 1; i++)
            {
                int doc_id;
                string doc_name;
                List1_type Tag;
                doc_id = (int)DT.Rows[i]["doctor_id"];
                doc_name = (string)DT.Rows[i]["Doctor_Name"].ToString();
                Tag.ID = doc_id;

                ListViewItem IT = listDocs.Items.Add("D" + doc_id.ToString(),doc_name,"");
                IT.Tag = doc_id;
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            DataTable DT = DB_BusinessRules.get_doctor_revenue();
            DG1.DataSource = DT;
            DG1.Refresh();
        }

        private void listDocs_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (listDocs.SelectedItems[0] == null) return;
                int D_ID = (int)listDocs.SelectedItems[0].Tag;
                para.select_doc_ID = D_ID;

            }
            catch (Exception ex)
            {
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            DataTable DT = DB_BusinessRules.finance_prescriptions(sdateT.Value,edateT.Value);
            DG1.DataSource = DT;
            DG1.Refresh();

        }

        private void btnVisits_NO_Click(object sender, EventArgs e)
        {
            string SQL = "";
            if (txtPatient_ID.Text != "")
            {
                SQL = "SELECT DISTINCT schedule.patient_id,COUNT(visit.visit_id) AS `Visit Count`,SUM(medical_service.price) AS `Total paid`" +
                "FROM service_doctor INNER JOIN medical_service ON (service_doctor.service_id = medical_service.service_id) INNER JOIN schedule ON (service_doctor.pair_id = schedule.pair_id) INNER JOIN visit ON (schedule.schedule_id = visit.schedule_id) " +
                "WHERE schedule.patient_id="+ txtPatient_ID.Text +" GROUP BY schedule.patient_id";

            }
            else
            {
                SQL = "SELECT DISTINCT schedule.patient_id,COUNT(visit.visit_id) AS `Visit Count`,SUM(medical_service.price) AS `Total paid`" +
                "FROM service_doctor INNER JOIN medical_service ON (service_doctor.service_id = medical_service.service_id) INNER JOIN schedule ON (service_doctor.pair_id = schedule.pair_id) INNER JOIN visit ON (schedule.schedule_id = visit.schedule_id) " +
                "GROUP BY schedule.patient_id";

            }

            DataTable DT = DBUtility.Get_Data(SQL);
            DG2.DataSource = DT;
            DG2.Refresh();
        }

        private void btn_visits_Click(object sender, EventArgs e)
        {
            string SQL = "";
            if (txtPatient_ID.Text != "")
            {
                SQL = "SELECT schedule.patient_id,medical_service.service_name,get_Person_name(service_doctor.doctor_id) as `seen by`," +
                " visit_id,visit.reason,visit.diagonosis,schedule.service_date,status_types.health_indication" +

                " FROM schedule INNER JOIN visit ON (schedule.schedule_id = visit.schedule_id) INNER JOIN status_types ON (visit.health_indication = status_types.status_id) INNER JOIN service_doctor ON (service_doctor.pair_id = schedule.pair_id) INNER JOIN medical_service ON (service_doctor.service_id = medical_service.service_id)" +
                " WHERE schedule.patient_id=" + txtPatient_ID.Text;
                
            }
            else
            {
                SQL = "SELECT schedule.patient_id,medical_service.service_name,get_Person_name(service_doctor.doctor_id) as `seen by`," +
                " visit_id,visit.reason,visit.diagonosis,schedule.service_date,status_types.health_indication" +

                " FROM schedule INNER JOIN visit ON (schedule.schedule_id = visit.schedule_id) INNER JOIN status_types ON (visit.health_indication = status_types.status_id) INNER JOIN service_doctor ON (service_doctor.pair_id = schedule.pair_id) INNER JOIN medical_service ON (service_doctor.service_id = medical_service.service_id)";
            }

            DataTable DT = DBUtility.Get_Data(SQL);
            DG2.DataSource = DT;
            DG2.Refresh();

        }

        private void btnPrescriptions_Click(object sender, EventArgs e)
        {
            string SQL = "";
            if (txtPatient_ID.Text != "")
            {
                SQL = "SELECT schedule.patient_id, visit.visit_id, prescription.perscription,  prescription.description, prescription.issue_Date, prescription.valid_untill" +
                " FROM prescription INNER JOIN visit ON (prescription.visit_id = visit.visit_id) INNER JOIN schedule ON (visit.schedule_id = schedule.schedule_id)" +
                " WHERE schedule.patient_id=" + txtPatient_ID.Text;

            }
            else
            {
                SQL = "SELECT schedule.patient_id, visit.visit_id, prescription.perscription,  prescription.description, prescription.issue_Date, prescription.valid_untill" +
                      " FROM prescription INNER JOIN visit ON (prescription.visit_id = visit.visit_id) INNER JOIN schedule ON (visit.schedule_id = schedule.schedule_id)";

            }

            DataTable DT = DBUtility.Get_Data(SQL);
            DG2.DataSource = DT;
            DG2.Refresh();

        }
        
    }
}
